package com.github.caijh.mall.system.controller;

import java.util.Map;
import javax.inject.Inject;

import com.github.caijh.framework.web.controller.BaseController;
import com.github.caijh.mall.system.request.SystemFormCheckReqBody;
import com.github.caijh.mall.system.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("api/admin/system/config")
@Api(tags = "设置 -- Config")
public class SystemConfigController extends BaseController {

    @Inject
    private SystemConfigService systemConfigService;

    /**
     * 查询配置表信息
     *
     * @param formId Integer
     * @return map
     * @author Mr.Zhang
     * @since 2020-04-13
     */
    @ApiOperation(value = "详情")
    @GetMapping(value = "/info")
    public Map<String, String> info(@RequestParam(value = "formId") Integer formId) {
        return systemConfigService.info(formId);
    }


    /**
     * 整体保存表单数据
     *
     * @param systemFormCheckReqBody 新增参数
     */
    @ApiOperation(value = "整体保存表单数据")
    @PostMapping(value = "/save/form")
    public void saveFrom(@RequestBody @Validated SystemFormCheckReqBody systemFormCheckReqBody) {
        systemConfigService.saveForm(systemFormCheckReqBody);
    }

    /**
     * 检测表单name是否存在
     *
     * @param name name
     */
    @ApiOperation(value = "检测表单name是否存在")
    @GetMapping(value = "/check")
    public void check(@RequestParam String name) {
        systemConfigService.checkName(name);
    }

    /**
     * 配置表中仅仅存储对应的配置
     *
     * @param key   配置表中的配置字段
     * @param value 对应的值
     */
    @ApiOperation(value = "表单配置中仅仅存储")
    @PostMapping(value = "/saveuniq")
    public void justSaveUniq(@RequestParam String key, @RequestParam String value) {
        systemConfigService.updateOrSaveValueByName(key, value);
    }

    /**
     * 根据key获取表单配置数据
     *
     * @param key 配置表的的字段
     * @return value of key
     */
    @ApiOperation(value = "表单配置根据key获取")
    @GetMapping(value = "/getuniq")
    public String justGetUniq(@RequestParam String key) {
        return systemConfigService.getValueByKey(key);
    }

}



